//package com.douqu.game.auth.platform;
//
//import com.alibaba.fastjson.JSONObject;
//import com.bean.core.util.HttpStringUtil;
//import com.bean.core.util.MD5Utils;
//import com.bean.core.util.TwoTuple;
//import com.douqu.game.auth.database.model.RoleRecordModel;
//import com.douqu.game.auth.service.PlayerService;
//import com.douqu.game.auth.utils.DesUtils;
//import com.douqu.game.core.e.ReturnMessage;
//import com.douqu.game.core.factory.ConstantFactory;
//import com.douqu.game.core.factory.PlatformFactory;
//import com.douqu.game.core.protobuf.SGCommonProto;
//import com.douqu.game.core.util.CoreUtils;
//import com.douqu.game.core.util.LogUtils;
//import com.douqu.game.core.yjsdk.utils.MD5;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import sun.misc.BASE64Decoder;
//import sun.misc.BASE64Encoder;
//
//import java.net.URLDecoder;
//import java.net.URLEncoder;
//import java.text.MessageFormat;
//
//
///**
// * Created by dengbo on 2018/9/11 0011.
// */
//@Component
//public class JTImpl implements PlatformSDK {
//    @Autowired
//    private PlayerService playerService;
//
//    @Override
//    public TwoTuple<RoleRecordModel, ReturnMessage> auth(int serverId, String account, String password, String deviceId) {
//        LogUtils.info("即拓登录参数:" + account + "  " + password);
//        //格式 version|componentCode|timeStamp|token
//        String[] param = password.split(ConstantFactory.DIVISION);
//        // 版本号默认为1.0
//        String version;
//        //商户名简写
//        String componentCode;
//        //时间戳
//        String timeStamp;
//        String token;
//        try {
//            version = param[0];
//            componentCode = param[1];
//            timeStamp = param[2];
//            token = param[3];
//        } catch (Exception e) {
//            e.printStackTrace();
//            LogUtils.error("参数解析错误:" + password);
//            return new TwoTuple<>(null, ReturnMessage.AUTH_FAILURE);
//        }
//        //认证编号
//        String authenticateCode = version + "|" + componentCode + "|" + timeStamp;
//        //加密
//        String authenticateCodes = DesUtils.encrypt(authenticateCode, PlatformFactory.JT_AKEY);
//        String url = MessageFormat.format(PlatformFactory.JT_CHECK_LOGIN_URL, version, componentCode, timeStamp, authenticateCodes, token);
//        LogUtils.info("即拓url:" + url);
//        String str = HttpStringUtil.sendHttpGet(url);
//        LogUtils.info("即拓str:" + str);
//        JSONObject result = JSONObject.parseObject(str);
//        JSONObject object;
//        JSONObject data;
//        String resultCode;
//        String resultMsg;
//        String userId;
//        try {
//            object = result.getJSONObject("result");
//            resultCode = object.getString("resultCode");
//            resultMsg = object.getString("resultMsg");
//            data = result.getJSONObject("data");
//            LogUtils.info("即拓返回值:resultCode:" + resultCode + " resultMsg: " + resultMsg + "data: " + data);
//        } catch (Exception e) {
//            LogUtils.error("即拓获取数据失败:" + account);
//            e.printStackTrace();
//            return new TwoTuple<>(null, ReturnMessage.AUTH_FAILURE);
//        }
//        if (!resultCode.equals("000000") || !resultMsg.equals("成功") || data.equals("")) {
//            LogUtils.error("即拓验证失败:" + account);
//            return new TwoTuple<>(null, ReturnMessage.AUTH_FAILURE);
//        }
//        try {
//            //data成功返回userid
//            userId = data.getString("userid");
//            LogUtils.info("即拓返回的userid:" + userId);
//            account = CoreUtils.createAccount(userId, SGCommonProto.E_CHANNEL_TYPE.CHANNEL_TYPE_JITUO);
//            RoleRecordModel record = playerService.getUserRecordByAccount(userId);
//            if (record == null) {//需要创建
//                record = new RoleRecordModel();
//                record.setAccount(account);
//                record.setChannel(SGCommonProto.E_CHANNEL_TYPE.CHANNEL_TYPE_JITUO_VALUE);
//                record.setServerId(serverId);
//                LogUtils.info("即拓平台第一次登录，创建用户");
//            }
//            return new TwoTuple<>(record, null);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new TwoTuple<>(null, ReturnMessage.AUTH_FAILURE);
//        }
//    }
//
//    public static void main(String[] args) {
//        try {
//            //测试数据(模拟百度支付参数)
//            JSONObject content = new JSONObject();
//            content.put("UID", "1");
//            content.put("MerchandiseName", "baiDu");
//            //编码:先urlEncoder在base64加密
//            String s = URLEncoder.encode(content.toJSONString(), "utf-8");
//            System.out.println("json格式的字符串 s=" + s);
//            //加密
//            BASE64Encoder encoder = new BASE64Encoder();
//            byte[] key = s.getBytes();
//            String en = encoder.encodeBuffer(key);
//            //最终从百度获得到的参数object:{"content":"JTdCJTIyVUlEJTIyJTNBJTIyMSUyMiUyQyUyMk1lcmNoYW5kaXNlTmFtZSUyMiUzQSUyMmJhaWR1\r\nJTIyJTdE\r\n"}
//            JSONObject object = new JSONObject();
//            object.put("content", en);
//            System.out.println("加密后的object:" + object);
//            //取出加过密的字符串
//            String contents = object.getString("content");
//            // 开始解密
//            BASE64Decoder decoder = new BASE64Decoder();
//            byte[] keys = decoder.decodeBuffer(contents);
//            String de  = new String(keys);
//            //urlDecoder解码
//            String con = URLDecoder.decode(de, "utf-8");
//            System.out.println("解码后的content:" + con);
//            //转换成json对象
//            JSONObject obj=  JSONObject.parseObject(con);
//            System.out.println("解码后的值obj:" + obj);
//            String uid= obj.getString("UID");
//            String MerchandiseName= obj.getString("MerchandiseName");
//            System.out.println("uid:" + uid);
//            System.out.println("MerchandiseName:" + MerchandiseName);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
//
//}
//
